<?php

namespace Beerus\Modules\DesignerCompetition\Actions;

use Doraemon\service\admin\HonorBind;
use Illuminate\Http\Request;
use Encore\Admin\Actions\RowAction;

class EditHonor extends RowAction
{
    public $name = '奖项查看/录入';

    public function handle($signId, Request $request)
    {
        $honorIds = $request->post('honor_ids');
        $signYear = $request->post('sign_year');
        $uid = $request->post('uid');
        // 将奖项录入
        $rs = HonorBind::enter($uid, $signId, $honorIds, $signYear);
        if ($rs == HonorBind::STATUS_LIMIT) {
            return $this->response()->error('有奖项已经超过最大数量');
        }
        if ($rs == HonorBind::STATUS_FAIL) {
            return $this->response()->error('作品奖项录取失败');
        }
        return $this->response()->success('奖项录取成功')->refresh();
    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function form()
    {
        // 获取所有可选奖项
        $honors = HonorBind::getAvailHonor($this->row->sign_year);
        $honorIds = explode(',', $this->row->honor_ids);
        $label = '报名id-' . $this->row->id . '获得奖项';
        $this->checkbox('honor_ids', $label)
            ->options($honors)->default($honorIds);
        // 隐藏
        $this->hidden('sign_year', '')->default($this->row->sign_year);
        $this->hidden('uid', '')->default($this->row->uid);
    }
}
